package goodroommate.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import goodroommate.dal.DatabaseSession;
import goodroommate.dao.IKeyDataDao;
import goodroommate.domain.KeyData;
import goodroommate.exception.DatabaseException;

public class KeyDataDao implements IKeyDataDao {

	private static KeyData getKeyDataResultSet(ResultSet rs) throws SQLException {
		KeyData keyData = new KeyData();
		keyData.setKey(rs.getString("Key"));
		keyData.setValue(rs.getString("Value"));
		return keyData;
	}

	@Override
	public KeyData getKeyDataByName(DatabaseSession dbsession, String name) throws DatabaseException {
		String sql = "SELECT * FROM keydata WHERE `Key`=?";
		try (ResultSet rs = dbsession.executeQuery(sql, name)) {
			if (rs.next())
				return getKeyDataResultSet(rs);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DatabaseException(e);
		}
		return null;
	}
}
